﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using DomainModel.Entities;
using DomainModel.Concrete;
using System.Web.Mvc;

namespace WebCore.Helpers
{
    public class UserHelper
    {

        public static bool IsAuthenticatedUser(int? userID)
        {
            if (string.IsNullOrEmpty(HttpContext.Current.User.Identity.AuthenticationType))
                return false;

            FormsIdentity formsIdentiy = ((FormsIdentity)HttpContext.Current.User.Identity);

            if (formsIdentiy == null)
                return false;

            string[] userData = formsIdentiy.Ticket.UserData.Split(',');

            // admin could edit users
            if (userData[1] == WebRoles.Admin.ToString())
                return true;

            // get the id and compare it
            return userID == int.Parse(userData[0]);
        }

        public static void IssueAuthTicket(User user, bool rememberMe)
        {
            // the openid will be the lookup field in the baseUsercontroller
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.OpenID,
                                                                 DateTime.Now, DateTime.Now.AddDays(100),
                                                                 rememberMe, user.ToString());

            string ticketString = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketString);
            if (rememberMe)
                cookie.Expires = DateTime.Now.AddDays(10);

            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string GetUserRoleFromIdentityTicket(FormsIdentity formsIdentiy)
        {
            string[] userData = formsIdentiy.Ticket.UserData.Split(',');

            // user or admin
            return userData[1];

        }

        public static int GetUserIDFromIdentityTicket(FormsIdentity formsIdentiy)
        {
            string[] userData = formsIdentiy.Ticket.UserData.Split(',');

            // user or admin
            return int.Parse(userData[0]);

        }

        public static bool IsCreatedByUserID(int userID)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                return false;

            if (string.IsNullOrEmpty(HttpContext.Current.User.Identity.AuthenticationType))
                return false;

            FormsIdentity formsIdentiy = ((FormsIdentity)HttpContext.Current.User.Identity);

            if (formsIdentiy == null)
                return false;

            string[] userData = formsIdentiy.Ticket.UserData.Split(',');

            // admin could edit users
            if (userData[1] == WebRoles.Admin.ToString())
                return true;

            // get the id and compare it
            return userID == int.Parse(userData[0]);
        }
        
        public static bool IsAdminUser()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                return false;

            if (string.IsNullOrEmpty(HttpContext.Current.User.Identity.AuthenticationType))
                return false;

            FormsIdentity formsIdentiy = ((FormsIdentity)HttpContext.Current.User.Identity);

            if (formsIdentiy == null)
                return false;

            string[] userData = formsIdentiy.Ticket.UserData.Split(',');

            // admin could edit
            if (userData[1].Equals(WebRoles.Admin.ToString(), StringComparison.InvariantCultureIgnoreCase))
                return true;

            return false;
        }

        #region event
        public static bool IsUserRegisteredWithEvent(HttpContext context, int eventID)
        {
            // make sure user is authenticated
            if (!context.User.Identity.IsAuthenticated)
                return false;

            int userID = GetUserIDFromIdentityTicket((FormsIdentity)context.User.Identity);

            if (userID == 0)
                return false;

            var rsvpReposotry = new EventsRSVPRepository();

            var item = rsvpReposotry.GetByEventIDAndUserID(eventID, userID);

            if (item == null)
                return false;

            return true;
        }

        #endregion
         

        /// <summary>
        /// only have 1-20 temp images in the cropped image folder.
        /// </summary>
        /// <returns></returns>
        public static string GetUserTempImage()
        {
            string imageName = string.Concat(Settings.UserTempImageNamePattern, "{0}.png");
            return string.Format(imageName,Utils.RandomNumber(1,20));
        }
       
        public static string initialName = "unnamed";

    }
}
